<template>
  <el-dialog
    append-to-body
    title="充值"
    :visible.sync="showDialog"
    :close-on-click-modal="false"
    width="45%"
    :before-close="handleClose"
  >
    <div>
      用户当前余额：<span style="color: #29ae6d">{{
        userAmount | showUserAmount
      }}</span>
    </div>
    <el-form
      :model="formData"
      ref="refRechargeForm"
      label-position="top"
      label-width="120px"
    >
      <el-form-item label="充值金额" label-position="top" :rules="[{required:true,message:'请输入充值金额'}]" prop="money">
        <el-input
          v-model="formData.money"
          placeholder="请输入充值金额，单次充入不能超过1000"
          @input="bindInputMoney"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="showDialog = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确认充值</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { inputDecimalData } from "@/utils/index";
import { paySubmit, payComplete } from "@/api/pay";
export default {
  name: "CompRecharge",
  data() {
    return {
      showDialog: false,
      requesting: false,
      userAmount: 0,
      id_public_user_info: "",
      formData: {
        money: "",
      },
    };
  },
  filters: {
    showUserAmount(val) {
      if (!val) {
        return parseFloat("0").toFixed(2);
      }
      return parseFloat(val).toFixed(2);
    },
  },
  methods: {
    bindInputMoney() {
      let val = inputDecimalData(this.formData.money);
      if (val > 1000) {
        val = 1000;
      }
      this.formData.money = val;
    },
    onShowRecharge(row) {
      console.log(row, "showRecharge>>>row");
      if (!row.id_public_user_info) {
        return;
      }
      this.userAmount = row.userAmount;

      this.id_public_user_info = row.id_public_user_info;

      this.formData.money = "";
      this.showDialog = true;
      this.$nextTick(() => {
        this.$refs.refRechargeForm.clearValidate();
      });
    },
    handleSubmit() {
      this.$refs.refRechargeForm.validate((valid) => {
        if (valid) {
          this.onSubmit();
        }
      });
    },
    async onSubmit() {
      if (this.requesting) {
        return;
      }
      this.requesting = true;
      try {
        let resPay = await paySubmit(
          this.Qs.stringify({
            invoke_method: "lock_price_before_pay",
            id_public_user_info: this.id_public_user_info,
            pre_cmd_para_pay_type: 1211,
            mid_table_remark_001: this.formData.money,
            mid_table_remark_002: this.userAmount,
            mid_table_remark_013:this.formData.money,
            mid_table_remark_004:1003,
            mid_table_remark_029:2111,
            mid_table_remark_041:1003
          })
        );
        console.log(resPay, "pay>>>>>resPay");
        let resPayComplete = await payComplete(
          this.Qs.stringify({
            invoke_method: "pay_method_all_in_one",
            pre_cmd_para_pay_lock_code:
              resPay.pre_cmd_para_pay_lock_code,
          })
        );
        this.$notify({
          title: "Success",
          message: "充值成功",
          type: "success",
          duration: 2000,
        });
        this.$emit("refreshAmount");
        this.requesting = false;
        this.showDialog=false
      } catch (error) {
        this.requesting = false;
      }
    },
    handleClose() {
      if (this.requesting) {
        this.$message.warning("数据正在提交中……");
        return;
      }
      this.showDialog = false;
    },
  },
};
</script>
<style lang="scss" scoped></style>
